﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Xml.Serialization;
using System.IO;
using System.Linq;

namespace DC.BLL
{
    public static class XmlProvider
    {
        private static string CardXmlFilePath;
        private static string SellerXmlFilePath;
        /// <summary>
        /// 优惠券List
        /// </summary>
        public static List<CouponItemCard> ItemCardList{get;set;}
        /// <summary>
        /// 供应商品牌
        /// </summary>
        public static List<CardSeller> CardSelllerList { get; set; }
        static XmlProvider()
        {
            CardXmlFilePath = HttpContext.Current.Server.MapPath("~/Admin/App/DiscountCoupon/Card.xml");
            SellerXmlFilePath = HttpContext.Current.Server.MapPath("~/Admin/App/DiscountCoupon/Seller.xml");

            ItemCardList = new List<CouponItemCard>();

            if (!System.IO.File.Exists(CardXmlFilePath))
            {
                File.Create(CardXmlFilePath);
            }

            if (!System.IO.File.Exists(SellerXmlFilePath))
            {
                File.Create(SellerXmlFilePath);
            }
        }
        /// <summary>
        /// 取所有Item
        /// </summary>
        /// <returns></returns>
        public static List<CouponItemCard> GetAllItemCard()
        {
            XmlSerializer ser = new XmlSerializer(typeof(List<CouponItemCard>));
            if (System.IO.File.Exists(CardXmlFilePath))
            {
                try
                {
                    StreamReader reader = new StreamReader(CardXmlFilePath);
                    ItemCardList = ser.Deserialize(reader) as List<CouponItemCard>;
                    ItemCardList = ItemCardList.OrderByDescending(o => o.CreateTime).ToList();
                    reader.Close();
                }
                catch { }
            }
            return ItemCardList;
        }
        /// <summary>
        /// 保存ItemCard
        /// </summary>
        public static void SaveAllItemCard()
        {
            XmlSerializer ser = new XmlSerializer(typeof(List<CouponItemCard>));
            if (System.IO.File.Exists(CardXmlFilePath))
            {
                try
                {
                    StreamWriter reader = new StreamWriter(CardXmlFilePath);
                    ser.Serialize(reader, ItemCardList);
                    reader.Close();
                }
                catch { }
            }
        }

        /// <summary>
        /// 取所有Seller
        /// </summary>
        /// <returns></returns>
        public static List<CardSeller> GetAllCardSeller()
        {
            XmlSerializer ser = new XmlSerializer(typeof(List<CardSeller>));
            if (System.IO.File.Exists(CardXmlFilePath))
            {
                try
                {
                    StreamReader reader = new StreamReader(SellerXmlFilePath);
                    CardSelllerList = ser.Deserialize(reader) as List<CardSeller>;
                    reader.Close();
                }
                catch { }
            }
            return CardSelllerList;
        }

        /// <summary>
        /// 保存Seller
        /// </summary>
        public static void SaveAllCardSeller()
        {
            XmlSerializer ser = new XmlSerializer(typeof(List<CardSeller>));
            if (System.IO.File.Exists(CardXmlFilePath))
            {
                try
                {
                    StreamWriter reader = new StreamWriter(SellerXmlFilePath);
                    ser.Serialize(reader, CardSelllerList);
                    reader.Close();
                }
                catch { }
            }
        }

    }
}
